package team.tinavv.ssheasy.core.util;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;

/**
 * 这是一个网络web交互的公共方法类
 * 
 * @author renjiangtang
 * 
 */
public class WebUtil {
	private static final Logger LOG = Logger.getLogger(WebUtil.class);

	// 私有化构造函数
	private WebUtil() {

	}

	/**
	 * 把对象转换为json形式返回
	 * 
	 * @author renjiangtang
	 * @param object
	 *            需要返回的对象
	 */
	public static void outputForJson(Object object) {
		String j = JSON.toJSONString(object);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		try {
			response.getWriter().write(j);
			response.getWriter().flush();
		} catch (IOException e) {
			LOG.warn("has error when out put json!");
			LOG.warn(e.getMessage(), e);
		}
	}
}
